var approval=null;

$(document).ready(function(){
    loadPage();
})

function loadPage() {
    var form;

    var userInfo=getUserByToken();
    userInfo={companyId:1,serviceId:2}
    $(".data_init_by_user").each(function () {
        $(this).val(userInfo[$(this).attr("name")]);
    })

    function initUploadedFiled(obj, data) {
        if(isNotBlank(data.fsId)){
            obj.find(".upload_show_status_class").hide();
            var upload_show_file_class=obj.find(".upload_show_file_class");
            upload_show_file_class.attr("src",data.fsUrl);
            upload_show_file_class.show();
        }else{
            obj.find(".upload_show_file_class").hide();
            var upload_show_status_class=obj.find(".upload_show_status_class");
            upload_show_status_class.attr("href","#");
            upload_show_status_class.css("color","red");
            upload_show_status_class.html(data.fileName);
            upload_show_status_class.show();
        }
        obj.find(".submit_file_id_class").val(data.fsId)
    }

    $("#approve_approval").load("approvalConfig.html","",function () {
        userInfo=getUserByToken();
        userInfo={"userName":"申请人姓名"}
        if(userInfo==null){
            layer.msg("未获取到用户信息！", {icon: 0,time: 2000});
            return
        }
        approval=initApproval([{name:userInfo["userName"],status:1,isApplyUser:true}]);
    })

    layui.use(['form'], function(){
        form = layui.form;
    });

    form.on('radio(approvalWay)', function(data){
        if(parseInt(data.value)==1){
            $("#img_upload_btn").closest(".layui-form-item").show();
        }else{
            $("#img_upload_btn").closest(".layui-form-item").hide();
        }
    });
    layui.use('upload', function () {
        layui.upload.render({
            elem: "#img_upload_btn",
            auto: true,
            size: 250, //限制文件大小，单位 KB
            accept: 'images',
            acceptMime: 'image/*',
            field:"file",
            headers: defaultHeaders(),
            data:{fileType:"img"},
            url:serverPre+"/manager/saas/v1/common/uploadFsFile",
            before: function(obj){
                initUploadedFiled($("#img_upload_btn").closest(".upload_body_div_class"),{fsId:"",fileName:"正在上传……"})
            },
            done: function(res, index, upload){
                if(res.code==0){
                    if(res.data&&isNotBlank(res.data.fsId)){
                        initUploadedFiled($("#img_upload_btn").closest(".upload_body_div_class"),res.data);
                    }else{
                        showMessage(0, "上传出错！", 2000);
                    }
                }else {
                    showMessage(0, res.msg, 2000);
                }
            }
            ,error: function(index, upload){
                showMessage(0, "上传出错！", 2000);
            }
        });
    });

    form.on('submit(submitBtn)', function(data){
        submitForm(data.field);
    });

    initProductSelect()

    form.render();

    function submitForm(form) {
        layer.confirm('确定提交审批流?', {btn: ['确定', '取消']}, function (index, layero) {
            layer.closeAll('dialog');
            var approverObj=approval.result();
            form.approverIds=approverObj.approvers;
            form.userInfoRos=approverObj.userInfos;

            if(!(isNotBlank(form.approvalWay)&&parseInt(form.approvalWay)==1)){
                delete form.enclosureId;
                delete form.enclosureName;
            }

            $.ajax({
                type: 'post',
                url: serverPre + "/manager/saas/v1/approval/save",
                dataType: 'json',
                headers: defaultPostHeaders(),
                async: false,
                cache: false,
                data:JSON.stringify(form),
                success: function (result) {
                    if (result.code == 0) {
                        layer.msg("提交审批流成功！", {icon: 1,time: 2000}, function () {
                            closeLayer(true)
                        });
                    } else {
                        layer.msg(result.msg, {icon: 0,time: 2000});
                    }
                }
            });
        });
    }

    function initProductSelect() {
        $.ajax({
            type: 'post',
            url: serverPre + "/api/saas/v1/product/getProductsByCIds",
            dataType: 'json',
            headers: defaultPostHeaders(),
            async: false,
            cache: false,
            data:JSON.stringify({companyIds:[userInfo.companyId],userId:userInfo.ehrId}),
            success: function (result) {
                if (result.code == 0&&result.data) {
                  $("#productId").empty();
                  var productSelect=$("#productId");
                    result.data.forEach(function (e) {
                        productSelect.append(new Option(e.name,e.id))
                    })
                } else {
                    layer.msg(result.msg, {icon: 0,time: 2000});
                }
            }
        });
    }
}

